# -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 10:46:06 2025

@author: Administrator
"""

import lr_func as fun
import re
import requests 
from concurrent.futures  import ThreadPoolExecutor 
import socket 
import json 


def get_ips():
    print('正在获取免费ip...')
    ip_lst=[]
    
    for i in range(1,6):
        url=f'https://www.kuaidaili.com/free/inha/{i}/'

        resp=fun.browser_automation(url)
        
        ip=re.findall('"ip": "([0-9.]+)"', resp)
        port=re.findall('"port": "([0-9]+)"', resp)
        for i,j in zip(ip,port):
            result=f'{i}:{j}'
            print(f'获取到-->{result}')
            ip_lst.append(result)
            
    print('获取ip完成,即将进行验证...')
    return ip_lst
   

    

 
def update_ip(ip_list, timeout=8, max_workers=5, retries=2):
    """
    高级代理IP验证与存储函数 
    
    参数：
    ip_list (list): 代理IP列表，格式["ip:port", ...]
    timeout (int): 单次检测超时时间（秒）
    max_workers (int): 最大并发检测数 
    retries (int): 失败重试次数 
    
    返回：
    list: 可用代理IP列表 
    """
    
    def validate_proxy(proxy):
        """执行代理有效性验证"""
        test_urls = [
            {'url': 'http://httpbin.org/ip',  'protocols': ['http', 'https']},
            {'url': 'https://www.google.com/gen_204',  'protocols': ['https']},
            {'url': 'http://www.baidu.com',  'protocols': ['http']}
        ]
 
        proxies = {
            'http': f'http://{proxy}',
            'https': f'https://{proxy}'
        }
        print(f'正在验证可用性-->{proxies}')
        for _ in range(retries + 1):
            for test in test_urls:
                try:
                    # 协议兼容性检测 
                    resp = requests.get( 
                        test['url'],
                        proxies={p: proxies[p] for p in test['protocols']},
                        timeout=timeout 
                    )
                    
                    # 深度有效性验证 
                    if resp.status_code  == 200:
                        if 'httpbin' in test['url']:
                            origin = json.loads(resp.text)['origin'] 
                            if proxy.split(':')[0]  in origin:
                                return proxy 
                        else:
                            return proxy 
                    elif 200 < resp.status_code  < 400:
                        return proxy 
                except (requests.exceptions.RequestException, 
                        socket.timeout, 
                        json.JSONDecodeError,
                        ConnectionResetError):
                    continue 
        return None 
 
    # 并发验证处理 
    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        results = list(executor.map(validate_proxy,  ip_list))
 
    # 结果处理与存储 
    valid_ips = list(filter(None, results))
    
    # 持久化存储
    print('验证完毕,正在将ip写入文件...')
    with open('ip_new.txt',  'w', encoding='utf-8') as f:
        f.write('\n'.join(valid_ips)) 
    
    return valid_ips 
 
# 使用
if __name__ == "__main__":
    proxy_list = get_ips()
    print(f'获取到ip列表:{proxy_list}')
    available = update_ip(proxy_list)
    print(f"可用代理数量：{len(available)}")
    print(available)
    print('使用:将生成的ip_new.txt文件重命名为ip.txt,放入lr_func/data文件夹中即可!')











